package cn.stronglink.asset.message.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import cn.stronglink.suite.core.util.PropertiesUtil;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
	
	//允许连接的域,只能以http或https开头
    String[] allowsOrigins = {PropertiesUtil.getString("cross.domain")};
	
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(handler(), "/message").setAllowedOrigins(allowsOrigins).addInterceptors(new WebSocketInterceptor()).withSockJS();	
	}
	
	@Bean
    public WebSocketHandler handler() {
        return new MessageHandler();
    }

}
